package com.ymatou.iapi.productimporter.testcase;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productimporter.parameter.CreateBean;
import com.ymatou.iapi.productimporter.parameter.GetTaskByIdListBean;
import com.ymatou.iapi.productimporter.parameter.TaobaoZhuShouExportBean;
import com.ymatou.iapi.productimporter.parameter.TaskInfo;
import com.ymatou.iapi.productimporter.service.GetTaskByIdListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductImporterCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.ProductImportDetail;
import com.ymttest.database.model.ProductImportTask;
import com.ymttest.database.sqlwapper.ProductImporterWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
*批量查询任务
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTaskByIdList{
	private static GetTaskByIdListBean gettaskbyidlistBean;
	private static GetTaskByIdListCall gettaskbyidlistCall;
	private static int vipRootUserId = AgentUsers.vipRootUserId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询任务");
	}

	@Before
	public void caseUp() {
		gettaskbyidlistBean=new GetTaskByIdListBean();
		gettaskbyidlistCall=new GetTaskByIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettaskbyidlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(GetTaskByIdListBean gettaskbyidlistBean){
		String taskIds = gettaskbyidlistBean.getTaskIdList().stream().collect(Collectors.toList()).toString();
		List<TaskInfo> taskInfos = gettaskbyidlistCall.getTaskInfos();
		ProductImporterWapper productImporterWapper = new ProductImporterWapper();
		taskInfos.forEach(p->{
			Logger.verifyEquals(true,taskIds.contains(p.getTaskId().toString()),"验证taskId");
			List<ProductImportDetail> successProduct = productImporterWapper.selectProductImportDetailByTaskIdWithIsSuccess(p.getTaskId(),true);
			List<ProductImportDetail> failProduct = productImporterWapper.selectProductImportDetailByTaskIdWithIsSuccess(p.getTaskId(),false);
			List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(p.getTaskId());
			ProductImportTask productImportTask = productImporterWapper.selectProductImportTaskByTaskId(p.getTaskId());

			Logger.verifyEquals(p.getTaskStatus(),productImportTask.getTaskStatus(),"验证taskStatus");
			Logger.verifyEquals(p.getFileName(),productImportTask.getLocalFilename(),"验证fileName");
			Logger.verifyEquals(p.getRemark(),productImportTask.getTaskStatusMsg(),"验证Remark");
			if (p.getTaskStatus()==1||p.getTaskStatus()==0){
				Logger.verifyEquals(p.getTotalNum(),0,"验证totalNum");
				Logger.verifyEquals(p.getSuccessNum(),0,"验证successNum");
			}else{
				Logger.verifyEquals(p.getTotalNum(),productImportDetails.size(),"验证totalNum");
				Logger.verifyEquals(p.getSuccessNum(),successProduct.size(),"验证successNum");
			}
			if (p.getFailNum()>0){
				Logger.verifyEquals(p.getFailNum(),failProduct.size(),"验证failNum");
			}
		});
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByIdList_01() {
		Logger.start(true,"批量查询任务,创建任务");
		try {
			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			int taskId = ProductImporterCallService.create(createBean);
			gettaskbyidlistBean.setTaskIdList(Arrays.asList(taskId));
			gettaskbyidlistBean.setAppId(TestDataManager.getRandomNum(7));
			gettaskbyidlistCall.setData(gettaskbyidlistBean);
			gettaskbyidlistCall.callService();			
			Logger.verifyEquals("true",gettaskbyidlistCall.getString("success"),"验证success");
			verifyDB(gettaskbyidlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByIdList_02() {
		Logger.start(true,"批量查询任务，执行任务中");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			List<TaobaoZhuShouExportBean> datas = new ArrayList<>();
			for (int i=0;i<10;i++){
				TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
				datas.add(tb);
			}
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl= TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());
			gettaskbyidlistBean.setTaskIdList(Arrays.asList(taskId));
			gettaskbyidlistBean.setAppId(TestDataManager.getRandomNum(7));
			gettaskbyidlistCall.setData(gettaskbyidlistBean);
			gettaskbyidlistCall.callService();
			Logger.verifyEquals("true",gettaskbyidlistCall.getString("success"),"验证success");
			verifyDB(gettaskbyidlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByIdList_03() {
		Logger.start(true,"批量查询任务，任务完成");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品"+TestDataManager.getRandomNum(6), "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(2000);
			gettaskbyidlistBean.setTaskIdList(Arrays.asList(taskId));
			gettaskbyidlistBean.setAppId(TestDataManager.getRandomNum(7));
			gettaskbyidlistCall.setData(gettaskbyidlistBean);
			gettaskbyidlistCall.callService();
			Logger.verifyEquals("true",gettaskbyidlistCall.getString("success"),"验证success");
			verifyDB(gettaskbyidlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}